<?php
/*
 module:		会员管理
 create_time:	2022-02-28 18:55:11
 author:	陕西格创
 contact:		
*/

namespace app\api\controller;

use alicloud\IdNumberCheck;
use app\api\service\MemberService;
use app\api\model\Member as MemberModel;
use app\backend\model\SysSite;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
use EasyWeChat\Factory;
use app\api\model\ZhBalanceLog;
use app\api\model\SysSite as SysSiteModel;
class ZhRealName extends Common {

    function test(){
        $realName = '张';
        $idCard = '362227199910062223';
        try {
            IdNumberCheck::check($realName, $idCard);
            return $this->ajaxReturn($this->successCode,'实名认证成功');
        }catch (\Exception $e){
            return $this->ajaxReturn($this->errorCode,$e->getMessage());
        }
    }
	
    /**实名认证**/
    function authentication(){
        $wxapp_id     = $this->request->post('wxapp_id');
        $uId          = $this->request->uid;
        $realName     = $this->request->post('real_name','','serach_in');
        $phone        = $this->request->post('phone','','serach_in');
        $idCard       = $this->request->post('id_card','','serach_in');
//        $pic          = $this->request->post('pic','','serach_in');
        if(!$wxapp_id){
            return $this->ajaxReturn($this->errorCode,'缺少平台参数');
        }
        $wxSetting    = SysSiteModel::where('wxapp_id',$wxapp_id)->find();
        if(!$wxSetting){
            return $this->ajaxReturn($this->errorCode,'平台参数未配置');
        }
        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();
        if(!$user){
            return $this->ajaxReturn($this->errorCode,'未知用户');
        }
        if(!$realName || !$idCard){
            return $this->ajaxReturn($this->errorCode,'参数错误');
        }

        try {
            IdNumberCheck::check($realName, $idCard);
        }catch (\Exception $e){//实名出错，用250
            $message = $e->getMessage();
            if($message == '其它错误'){
                $message = '身份证号码和姓名不符，请修改!';
            }
            return $this->ajaxReturn(250, $message);
        }


        $data = [
            'real_name'  => $realName,
            'id_card'    => $idCard,
//            'pic'        => $pic,
            'phone'      => $phone,
            'is_auth'   => 1, //变更为已实名
            'auth_status'=> 2//调用审核之前小程序走了实名认证，默认自动通过
        ];
        $res = MemberModel::where(['wxapp_id'=> $wxapp_id,'uid'=> $uId])->update($data);
        if($res){
            return $this->ajaxReturn($this->successCode,'提交审核成功');
        }else {
            return $this->ajaxReturn($this->errorCode,'提交审核失败');
        }
    }
    
    /**获取实名认证信息**/
    function getAuthInfo(){
        $wxapp_id     = $this->request->get('wxapp_id');
        $uId          = $this->request->uid;
        if(!$wxapp_id){
            return $this->ajaxReturn($this->errorCode,'缺少平台参数');
        }
        $wxSetting    = SysSiteModel::where('wxapp_id',$wxapp_id)->find();
        if(!$wxSetting){
            return $this->ajaxReturn($this->errorCode,'平台参数未配置');
        }
        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->field('uid,phone,id_card,real_name,auth_status')->find();
        if(!$user){
            return $this->ajaxReturn($this->errorCode,'未知用户');
        }else {
            return $this->ajaxReturn($this->successCode,'成功',$user);
        }
        
    }
 
}

